<?php
include 'AESTool.php';
include 'RSATool.php';

function getRandomStr( $length) {
    // 密码字符集，可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $password;
}

function curl_request($url,$post='',$cookie='', $returnCookie=0){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
    if($post) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if($cookie) {
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    if (curl_errno($curl)) {
        return curl_error($curl);
    }
    curl_close($curl);
    if($returnCookie){
        list($header, $body) = explode("\r\n\r\n", $data, 2);
        preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
        $info['cookie']  = substr($matches[1][0], 1);
        $info['content'] = $body;
        return $info;
    }else{
        return $data;
    }
}

$sn = getRandomStr(28);
echo 'sn : '.$sn."<br>";
//$sn = 'LCTeyk1sP0JgxzEC9EH8v2nvRVFs';
$merchant = "1003170422209138";
$deviceInfo = "device1";
$totalAmount = "0.01";
$subject = "通用商户收款";
$callBack = "http://www.baidu.com";
$returnURL = "DEFAULT";
$channel = "weixin";
$terminalType = "mobile";
$tradeIP = "119.136.147.33";
$remark = "buy apple";

echo mb_internal_encoding().'<br>';

$signContent = $sn.$merchant.$deviceInfo.$totalAmount.$subject.$callBack.$returnURL.$channel.$terminalType.$tradeIP.$remark;
$key = "T6B979AF";
$signContent = $signContent.$key;
// echo 'signContent : '.$signContent.'<br>';

$sign = md5($signContent);
echo '<br>sign:'.$sign.'<br><br>';

$busiType = "100001";
$encryContent = $sign.$busiType;
// $random16 = getRandomStr(16);
$random16 = 'v4lKsPKs89yRFvUk';
// echo 'random16:'.$random16.'<br><br>';

$aes = new AESTool();
$encryptData = $aes->encrypt($encryContent, $random16);
// echo 'AES encrypt : ' . $encryptData . '<br><br>';

$rsaTool = new RSAUtils();
$publicKeyPath = '/Users/leiyan/Sites/Document/payment/cer/test1_rsa_public_key.pem';
$encryptKey = $rsaTool->ecbEncrypt($random16, $publicKeyPath);
// echo 'key : '.$random16.'<br>';
// echo 'RSAencryptKey : '.$encryptKey.'<br><br>';

$privateKeyPath = '/Users/leiyan/Sites/Document/payment/cer/1003170422209138rsaPrivate.p12';
$privatePwd = '12345678';
$digitalSign = $rsaTool->sign($sign, $privateKeyPath, $privatePwd);
echo 'rsaSign : '.$digitalSign.'<br><br>';

$paramMap = array();
$paramMap["sn"] = $sn;
$paramMap["merchant"] = $merchant;
$paramMap["deviceInfo"] = $deviceInfo;
$paramMap["totalAmount"] = $totalAmount;
$paramMap["subject"] = $subject;
$paramMap["callBack"] = $callBack;
$paramMap["returnURL"] = $returnURL;
$paramMap["channel"] = $channel;
$digitalSign = urlencode($digitalSign);
$encryptData = urldecode($encryptData);
$encryptKey = urlencode($encryptKey);
$paramMap["sign"] = $digitalSign;
$paramMap["encryptData"] = $encryptData;
$paramMap["encryptKey"] = $encryptKey;
$paramMap["busiType"] = $busiType;
$paramMap["terminalType"] = $terminalType;
$paramMap["tradeIP"] = $tradeIP;
$paramMap["remark"] = $remark;	

// var_dump($paramMap);

$url = "http://120.76.192.159:8170/BusiM/AL001/Check";
// $result = curl_request($url,$paramMap);
// echo 'post result : '. $result;

$curlPost = 'merchant='.$merchant.'&sn='.$sn.'&deviceInfo='.$deviceInfo.'&totalAmount='.$totalAmount.'&subject='.$subject.'&callBack='.$callBack.'&returnURL='.$returnURL.'&channel='.$channel.'&terminalType='.$terminalType.'&tradeIP='.$tradeIP.'&busiType='.$busiType.'&remark='.$remark.'&sign='.$digitalSign.'&encryptData='.$encryptData.'&encryptKey='.$encryptKey;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://120.76.192.159:8170/BusiM/AL001/Check');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
$data = curl_exec($ch); 
curl_close($ch);
echo 'Req Data:'.$data.'</br>';

// $dataJson = json_decode($data);
// $codeRsp = $dataJson->codeRsp;
// echo 'codeRsp:'.json_encode($codeRsp).'</br>';
// $encrypt = $dataJson->encrypt;
// if ($encrypt){
//     $en_encryptKey = $encrypt->encryptKey;
//     $en_encryptData = $encrypt->encryptData;
//     echo 'en_encryptKey:'.$en_encryptKey.'</br>';
//     echo 'en_encryptData:'.$en_encryptData.'</br>';
    
//     $deKeyStr16 = decode_rsa(urldecode($en_encryptKey));
//     echo '</br>'.'deKeyStr16:'.$deKeyStr16.'</br>';
    
//     $deData = decode_aes($deKeyStr16,urldecode($en_encryptData));
//     echo 'deData:'.$deData.'</br>';
    
//     echo "--:".substr($deData,0,strrpos($deData,'}')+1);
//     $dataJsonRs = json_decode(substr($deData,0,strrpos($deData,'}')+1));
//     echo 'code:'.$dataJsonRs->code.'</br>';
// }
?>



